<?php
require_once "utils/db.php";
require_once "utils/util.php";
//echo $_REQUEST['__id'];

$qStringFieldsProperties =
			"
			SELECT 
				Entity.`id` as EntityId,
				Entity.`name` as EntityName,
				Entity.`table_name` as EntityTable,
				Field.`id` as FieldId,
				Field.`name` as FieldName,
				Field.`type` as FieldType,
				Field.`db_field` as FieldDBField,
				Property.`property_name` as PropertyName
			FROM 
				`entities` AS Entity, 
				`entity_fields` AS Field
				LEFT JOIN `field_properties` Property ON Property.`field_id` = Field.`id`
			WHERE
				Entity.`id` = ".$_REQUEST['__id']." AND
				Field.`entity_id` = Entity.`id`
			ORDER BY Field.`form_location`
			";
			/*
			"
			SELECT 
				Entity.`id` as EntityId,
				Entity.`name` as EntityName,
				Entity.`table_name` as EntityTable,
				Field.`id` as FieldId,
				Field.`name` as FieldName,
				Field.`type` as FieldType,
				Field.`db_field` as FieldDBField,
				Property.`property_name` as PropertyName
			FROM 
				`entities` AS Entity, 
				`entity_fields` AS Field
				LEFT JOIN `field_properties` Property ON Property.`field_id` = Field.`id`
				LEFT JOIN 'grid_properties' GProperty ON GProperty.'field_id' = Field.`id`
			WHERE
				Entity.`id` = ".$_REQUEST['__id']." AND
				Field.`entity_id` = Entity.`id`
			ORDER BY Field.`form_location`
			";
			*/
$qStringGridColumns = 
			"
			SELECT
				Entity.`table_name` as EntityTable,
				Field.* 
			FROM 
				`entities` AS Entity,
				`entity_fields` AS Field
			WHERE 
				Field.`entity_id` = ".$_REQUEST['__id']." AND 
				Entity.`id` = ".$_REQUEST['__id']." AND
				Field.`grid_location` > 0 
			ORDER BY Field.`grid_location`
			";
$db = new db();
	$fieldProperties = $db->rawSelect($qStringFieldsProperties);
	$gridColumns = $db->rawSelect($qStringGridColumns);
$db->close();
// used to format grid columns
$columns = ""; 
$storeDef = "";
// used for building the selection of columns in data query 
$gridDataQueryFields = "";
$database = $gridColumns[0]['EntityTable'];

// used to build retriving query for store
$gridDataQuery = "SELECT ";
foreach($gridColumns as $column)
{
//	$columns .= ",{id:'".$column['db_field']."',header: '".$column['name']."', sortable: true, dataIndex: '".$column['db_field']."'}";
	$columns .= ",{header:'".$column['name']."',sortable:true, dataIndex:'".$column['db_field']."'}";
	$storeDef .= ",{name:'".$column['db_field']."'}";
	$gridDataQueryFields .= ", `".$database."`.`".$column['db_field']."`";
}
$storeDef .= ",{name:'id'}";
$gridDataQuery .= substr($gridDataQueryFields, 1);
$gridDataQuery .= ", `".$database."`.`id` FROM `".$database."`";
$db = new db();
	$gridData = $db->rawSelect($gridDataQuery);
$db->close();
$store = "";
foreach($gridData as $row)
{
	$storeItem = "";
	foreach($row as $rowItem)
	{
		//$storeItem .= ", '". addslashes(strip_tags(str_ireplace("\r\n", " ", $rowItem)))."'";
		$storeItem .= ", '". addslashes(str_ireplace("\r\n", " ", $rowItem))."'";
	}
	$store .= ",[".substr($storeItem, 1)."]";
}
$columns = "[".substr($columns, 1)."]";
$storeDef = "[".substr($storeDef, 1)."]";
$store = "[".substr($store, 1)."]";

$gridDefinition[0] = $columns;
$gridDefinition[1] = $store;
$gridDefinition[2] = $storeDef;
print json_encode($gridDefinition);
//print nl2br(substr($columns, 1));
/*
print(
	"[
		['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
		['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am']
	]"
);
*/
?>